<template>
  <div class="mod-config">
    <basic-container>
      <div class="boxsty zhiBiao">
        <div class="flex justifyContentSpaceBetween">
          <div class="tabletext">述职管理</div>
          <div class="tablelink">
            <el-link icon="el-icon-plus" v-if="permissions.sysnorm_sysnorm_add" type="primary"
              @click="addOrUpdateHandle()" :underline="false">新增述职信息</el-link>
          </div>
        </div>
        <el-form :inline="true" :model="dataFormSzbg" @keyup.enter.native="getDataList()" class="form-inline">
          <el-form-item label="述职人名称">
            <el-input v-model="dataFormSzbg.szUserName" placeholder="请输入述职人名称"></el-input>
          </el-form-item>
          <el-form-item  >
            <el-button icon="el-icon-search" v-if="permissions.sysnorm_sysnorm_view" type="primary" class="marginLeft"
              @click="getDataList()">检索</el-button>
          </el-form-item>
        </el-form>

        <div>
          <el-table :data="dataList" v-loading="dataListLoading" stripe class="avue-crud" height="60vh">
            <el-table-column prop="" label="序号" :formatter="nShowIndex" width="80px"></el-table-column>
            <el-table-column prop="fqUserName" label="发起人">
            </el-table-column>
            <el-table-column prop="szLx" label="述职类型">
            </el-table-column>
            <el-table-column prop="szDeptName" label="述职部门">
            </el-table-column>
            <el-table-column prop="szUserName" label="述职人">
            </el-table-column>
            <el-table-column prop="szJzDate" :formatter="formater"  label="述职截止时间" width="180px">
            </el-table-column>
            <el-table-column prop="shUserName" label="审核人" width="120px">
            </el-table-column>
            <el-table-column prop="shZt" label="审核状态">
              <template slot-scope="scope">
                    <span :class="Zt(scope.row.shZt)">{{scope.row.shZt}}</span>
                  </template>
            </el-table-column>
            <el-table-column  width="260px" label="操作" >
              <template slot-scope="scope">
                <div  style="display: flex;">
                <el-upload    class="upload-demo" accept=".pdf,.doc,.docx,.PDF" :headers="headers" :show-file-list="false"
                  action="/admin/sys-file/upload" :on-success="handleUploadSuccess" :before-upload="beforeFileUpload "
                  :on-change="handleChange" :on-preview="handlePictureCardPreview">
                  <el-button  @click="getSzbgId(scope.row.id)" type="primary" plain>上传报告</el-button>
                </el-upload>
                <el-button class="ml10" v-if="scope.row.szbgFjdz" type="primary" plain
                  @click="handlePictureCardPreview(scope.row.szbgFjdz)">查看
                </el-button>
                <el-button class="ml10" type="danger" plain
                  @click="deleteHandle(scope.row.id)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>

        </div>

        <div class="avue-crud__pagination">
          <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background
            layout="total, sizes, prev, pager, next, jumper">
          </el-pagination>
        </div>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
      <el-dialog title="述职报告" :visible.sync="bgdialogVisible" width="80vw" top="3vh" append-to-body>
        <iframe :src="bgUrl" style="width: 100%;height:75vh"></iframe>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" plain @click="bgdialogVisible = false">取消</el-button>
        </span>
      </el-dialog>
    </basic-container>
  </div>
</template>

<script>
  import {
    fetchList,
    delObj,
    putObj
  } from '@/api/ceping/cepingszbgb'
  import TableForm from './cepingszbgb-form'
  import store from '@/store'
  import { getStore, setStore } from "@/util/store";
  import {
    getPreview
  } from "@/util/util";
  import {
    mapGetters
  } from 'vuex'
  export default {
    data() {
      return {
        dataFormSzbgSzbg: {
          id: 0,
          szbgFjmc: '',
          szbgFjlx: '',
          szbgFjdz: '',
        },
        bgdialogVisible: false,
        dataFormSzbg: {
          szUserName: '',
        },
        headers: {
          'TENANT-ID': getStore({ name: "tenantId" })
              ? getStore({ name: "tenantId" })
              : "1",
          Authorization: 'Bearer ' + store.getters.access_token
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        bgUrl: ''
      }
    },
    components: {
      TableForm
    },
    created() {
      this.getDataList()
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    methods: {
           formater(row, column, cellValue, index){
         if(cellValue){
        return this.$moment(cellValue).format("yyyy-MM-DD HH:mm");
      }else {
        return cellValue;
      }
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        fetchList(Object.assign({
          current: this.pageIndex,
          size: this.pageSize
        }, this.dataFormSzbg)).then(response => {
          this.dataList = response.data.data.records
          this.totalPage = response.data.data.total
        })
        this.dataListLoading = false
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        this.$confirm('确人删除该数据？' , '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delObj(id)
        }).then(data => {
          this.$notify.success('删除成功')
          this.getDataList()
        })
      },

      nShowIndex(row, column, cellValue, index) {
        return index + 1;
      },
      getSzbgId(szbgId) {
        this.dataFormSzbgSzbg.id = szbgId
      },
      /**
       * 文件上传成功
       */
      handleUploadSuccess(response, file, fileList) {

        if (response.code == 0) {
          this.dataFormSzbgSzbg.szbgFjdz = response.data.url;
          this.dataFormSzbgSzbg.szbgFjmc = file.name;
          this.dataFormSzbgSzbg.szbgFjlx = file.raw.type;
          putObj(this.dataFormSzbgSzbg).then(data => {
            this.$notify.closeAll();
            this.$notify.success('上传成功');
            this.getDataList();
            this.$emit("reload");

          }).catch(() => {
            this.$notify.closeAll();
            this.$notify.error('上传失败');
          });

        } else {
          this.$notify.closeAll();
          this.$notify.error('上传出错：' + response.msg)
        }
      },
      /**
       * 文件预览预留
       */
      handlePictureCardPreview(url) {
        if (url) {
          this.bgUrl = getPreview(url) + "&officePreviewType=pdf";
          this.bgdialogVisible = true;
        } else {
          this.$notify.closeAll();
          this.$notify.error('还未上传附件');
        }

      },
      /**
       * 文件修改
       */
      handleChange(file, fileList) {
        this.fileList = [];
        this.fileList.push(file);
      },
      /**
       * 文件上传前
       */
      beforeFileUpload(file) {
        const isType = file.type === 'application/pdf' || file.type === 'application/msword' || file.type ===
          'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
        if (!isType) {
          this.$notify.closeAll();
          this.$notify.error('上传文件格式不对!')
          return false;
        }
        return isType;
      },
      //状态
    Zt(value) {
      return {
        red: value === "审核中",
        green: value === "已通过",
        // bule: value === "审核中"
        // blue: value === "002",
        // green: value === "003",
      };

    }
    }
  }

</script>
